// components/order/order-list-item/index.js
const componentBehaviors = require('../../../../behaviors/component');
import { COMPONENT_OPTIONS } from '../../../../constants/common';
import { PACKAGE_ACTIVITY_PAGE } from '../../../../constants/route';
import { handleBrandOrderStatus, handleBrandAfterSaleOrderStatus } from '../../../biztools/order';
import { AGREE_BTN_TEXT, REFUSE_BTN_TEXT, ORDER_TYPE } from '../../../../constants/order';
import { navTo } from '../../../../common/env';
import { formatDate } from '../../../../common/utils';

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    properties: {
        // 订单信息
        orderInfo: {
            type: Object,
            value: {},
        },
        // 是否是售后单
        isAfterSale: {
            type: Boolean,
            value: false,
        },
    },
    observers: {
        'orderInfo, isAfterSale': function(info, flag) {
            const orderStatusInfo = flag ? handleBrandAfterSaleOrderStatus(info) : handleBrandOrderStatus(info);
            this.setData({
                orderStatusInfo,
                orderTime: formatDate(flag ? info.applied : info.ordered),
                isHaveDivider: this.dealIsHaveDivider(flag, orderStatusInfo),
            });
        },
    },
    data: {
        ORDER_TYPE,
        orderStatusInfo: handleBrandOrderStatus({}),
        orderTime: '',
        agreeBtnText: AGREE_BTN_TEXT,
        refuseBtnText: REFUSE_BTN_TEXT,
        isHaveDivider: false,
    },
    methods: {
        dealIsHaveDivider: function (isAfterSale, orderStatusInfo) {
            if (isAfterSale) {
                return orderStatusInfo.isHaveDealAction || orderStatusInfo.isHaveExpressAction;
            } else {
                return orderStatusInfo.isHaveComplate || orderStatusInfo.isHaveConfirm;
            }
        },
        goToOrderDetail: function () {
            this.triggerEvent('detail', this.data.orderInfo);
        },
        // 售后相关的方法
        // 拒绝申请
        refuseAction: function() {
            this.triggerEvent('refuse', this.data.orderInfo);
        },
        // 同意申请
        agreeAction: function() {
            this.triggerEvent('agree', this.data.orderInfo);
        },
        // 拒绝入库
        refuseWarehouseAction: function () {
            this.triggerEvent('refuseWarehouse', this.data.orderInfo);
        },
        // 同意入库
        agreeWarehouseAction: function () {
            this.triggerEvent('agreeWarehouse', this.data.orderInfo);
        },
        // 正常单的操作
        // 确认发货、确认备货
        confirmAction: function() {
            this.triggerEvent('confirm', this.data.orderInfo);
        },
        // 确认提货
        complateAction: function () {
            this.triggerEvent('complate', this.data.orderInfo);
        },
        // 常驻操作
        // 联系买家
        contactAction: function() {
            this.triggerEvent('contact', this.data.orderInfo);
        },
        goToActivity() {
            const { activityId, orderType } = this.data.orderInfo;
            if (orderType === ORDER_TYPE.SALE) {
                navTo(PACKAGE_ACTIVITY_PAGE['detail'], {
                    activityId,
                });
            }
        },
    }
})
